package com.lxy.equipment.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lxy.common.domain.common.R;
import com.lxy.common.domain.vo.AlertsVO;
import com.lxy.common.domain.vo.RobotVO;
import com.lxy.equipment.domain.po.Alerts;
import com.lxy.equipment.domain.po.Repair;
import com.lxy.equipment.mapper.AlertsMapper;
import com.lxy.equipment.service.AlertsService;
import com.lxy.equipment.service.RepairService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


@Slf4j
@Service
@RequiredArgsConstructor
public class AlertsServiceImpl extends ServiceImpl<AlertsMapper, Alerts> implements AlertsService {

    private final RepairService repairService;

    @Override
    public R sendAlerts(RobotVO robotVO) {
        //生成预警单
        Alerts alerts =new Alerts();
        //封装数据
        alerts.setRobotId(robotVO.getId());
        alerts.setEquipmentId(robotVO.getEquipmentId());
        alerts.setCreateTime(LocalDateTime.now());
        //存储数据
        save(alerts);
        return R.ok();
    }

    @Override
    public R<List<AlertsVO>> showAlerts() {
        List<Alerts> alerts = list();
        List<AlertsVO> alertsVOS=new ArrayList<>();
        //封装数据
        for (Alerts alert : alerts) {
            AlertsVO alertsVO=new AlertsVO();
            BeanUtils.copyProperties(alert,alertsVO);
            alertsVOS.add(alertsVO);
        }
        BeanUtils.copyProperties(alerts,alertsVOS);
        return R.ok(alertsVOS);
    }

    @Override
    public R alertUser(Long alertsId, Long userId) {
        //生成维修单
        Repair repair=new Repair();
        Alerts alerts = getById(alertsId);
        repair.setEquipmentId(alerts.getEquipmentId());
        repair.setUserId(userId);
        repair.setCreateTime(LocalDateTime.now());
        repair.setStatus(0);
        repair.setRemark("巡检机器人预警");
        //存储数据
        repairService.save(repair);
        //删除alerts数据
        removeById(alertsId);
        return R.ok();
    }
}
